//
//  GoodSectionPresenter.m
//  GRFMDownloader_Example
//
//  Created by Guo Rui on 2018/1/12.
//  Copyright © 2018年 GR. All rights reserved.
//

#import "GoodSectionPresenter.h"


@interface GoodSectionPresenter()

@property(nonatomic,weak)UICollectionView *collecionView;
@property (nonatomic, strong) NSMutableArray  *cellPArray;
@end

@implementation GoodSectionPresenter
@synthesize dataArray = _dataArray;

+ (instancetype)presentWithView:(UICollectionView *)collectionView{
    
    GoodSectionPresenter *p = [[GoodSectionPresenter alloc] init];
    p.collecionView = collectionView;
    return p;
}

- (NSInteger)itemCount
{
    return self.dataArray.count;
}

- (UICollectionReusableView *)headerViewWithCollectionView:(UICollectionView *)collectionView indexpath:(NSIndexPath *)indexPath
{
    UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:NSStringFromClass([UICollectionReusableView class]) forIndexPath:indexPath];
    headerView.backgroundColor = [UIColor blueColor];
    UILabel *label = [[UILabel alloc]init];
    label.frame = CGRectMake(0, 0, 0, 0);
    label.text = @"限时抢购";
    [label sizeToFit];
    label.backgroundColor = [UIColor orangeColor];
    [headerView addSubview: label];
    return headerView;
}

- (void)setDataArray:(NSArray *)dataArray
{
    _dataArray = dataArray;
    
    for (GroupBuyingBookModel *model in dataArray) {
        [self.cellPArray addObject:[[GoodCellPresenter alloc] initWithModel:model]];
    }
}

- (CGSize)sizeForItemAtIndex:(NSInteger)index
{
    if (self.cellPArray.count == 0) {
        return CGSizeZero;
    }
    
    GoodCellPresenter *p = [self.cellPArray objectAtIndex:index];
    return CGSizeMake(375, [p cellHeight]);
}

- (UICollectionViewCell<GoodCellProtocol> *)collectionView:(UICollectionView *)collectionView  forIndexPath:(NSIndexPath *)indexPath
{
    GoodTableViewCell *cell = (GoodTableViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass(self.class) forIndexPath:indexPath];

    GoodCellPresenter *p = [self.cellPArray objectAtIndex:indexPath.row];
    [p presentWithView:cell];
    
    return cell;
}

- (NSMutableArray *)cellPArray
{
    if (!_cellPArray) {
        _cellPArray = [NSMutableArray arrayWithCapacity:0];
    }
    return _cellPArray;
}
@end
